<?php

if (!function_exists('capture_template')) {

    function capture_template($template_name, $vars = array()) {
        ob_start();
        foreach ($vars AS $key => $val) {
            $$key = $val;
        }
        if (file_exists(TEMPLATEPATH . "/helpers/templates/$template_name.php")) {
            require_once (TEMPLATEPATH . "/helpers/templates/$template_name.php");
        } else {
            echo "<p>Rendering of template $template_name.php failed</p>";
        }
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }

}

function get_posts_count_from_today($post_type = 'post') {
    global $wpdb;
    $current_timestamp = current_time('timestamp');
    //$wpdb->show_errors();
    $sql = "SELECT {$wpdb->posts}.* " .
            "FROM {$wpdb->posts} " .
            "WHERE post_status='publish' " .
            "AND post_type= '" . $post_type . "' " .
            "AND DATE_FORMAT(post_date, '%Y-%m-%d') = '" . date('Y-m-d', $current_timestamp) . "' order by post_date desc";
    $posts = $wpdb->get_results($sql);
//    var_dump($posts);
//    echo $wpdb->posts;
//    echo $wpdb->last_query;
    //if($posts==NULL) return false;
    return $posts;
}

function get_posts_count_from_past($post_type = 'post') {
    global $wpdb;
    $current_timestamp = current_time('timestamp');
    $sql = "SELECT {$wpdb->posts}.* " .
            "FROM {$wpdb->posts} " .
            "WHERE post_status='publish' " .
            "AND post_type= '" . $post_type . "' " .
            "AND DATE_FORMAT(post_date, '%Y-%m-%d') < '" . date('Y-m-d', $current_timestamp) . "' order by post_date desc";
    $posts = $wpdb->get_results($sql);
//    $posts = $wpdb->get_results(
//            $wpdb->prepare(
//                    "SELECT * " .
//                    "FROM {$wpdb->posts} " .
//                    "WHERE post_status='publish' " .
//                    "AND post_type= %s " .
//                    "AND DATE_FORMAT(post_date, '%Y-%m-%d') < %s", $post_type, date('Y-m-d', time())
//            )
//    );
    return $posts;
}

?>
